﻿using AbpDemo.Authors.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace AbpDemo.Authors
{
    [ApiExplorerSettings(GroupName = "v2")]
    [Authorize]
    public class AuthorSerivce : CrudAppExtService<Author, AuthorDto, Guid, CreateUpdateAuthorDto>, IAuthorSerivce
    {
        private readonly IRepository<Author, Guid> repository;

        public AuthorSerivce(IRepository<Author, Guid> repository) : base(repository)
        {
            this.repository = repository;
        }

        public override async Task<ResultDto<AuthorDto>> CreateAsync(CreateUpdateAuthorDto createInput)
        {
            var author = await repository.FirstOrDefaultAsync(m=>m.AuthorName == createInput.AuthorName);
            if(author != null)
            {
                return new ResultDto<AuthorDto> { Code = ResultState.Failed, Message = "重名了" };
            }

            return await base.CreateAsync(createInput);
        }
    }
}
